
#include "nvs_dev_info.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "utils_log.h"

static const char *TAG = "NVS_DEV_INFO_CFG--->";

#define NVS_DEV_INFO_FILE_NAME      "DevInfo_cfg"
#define DEV_TYPE_KEY                "devType"
#define PRODUCT_ID_KEY              "productID"
#define DEV_ID_KEY                  "devID"
#define AIR_CON_GATE_INFO_KEY       "airConGateInfo"
#define METER_INFO_KEY              "meterInfo"

static int GetNvsData(const char *key, void *value, const int len)
{
    if (NULL==key || NULL==value){
        ESP_LOGE(TAG, "input params is invalid");
        LOG_TO_FILE("input params is invalid");
        return -1;
    }

    nvs_handle handle;
    esp_err_t err = nvs_open(NVS_DEV_INFO_FILE_NAME, NVS_READWRITE, &handle);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "open %s file failed:%s",NVS_DEV_INFO_FILE_NAME, esp_err_to_name(err));
        LOG_TO_FILE("open %s file failed:%s",NVS_DEV_INFO_FILE_NAME, esp_err_to_name(err));
        return -1;
    }
    
    err = nvs_get_blob(handle, key, value, &len);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "get %s blob failed:%s", key, esp_err_to_name(err));
    }

    nvs_close(handle);

    return err;
}

static int SetNvsData(const char *key, const void *value, const int len)
{
    if(NULL==key || NULL==value){
        ESP_LOGE(TAG, "input params is invalid");
        return -1;
    }

    nvs_handle handle;
    esp_err_t err = nvs_open(NVS_DEV_INFO_FILE_NAME, NVS_READWRITE, &handle);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "open %s file failed:%s",NVS_DEV_INFO_FILE_NAME, esp_err_to_name(err));
        LOG_TO_FILE("open %s file failed:%s",NVS_DEV_INFO_FILE_NAME, esp_err_to_name(err));
        return -1;
    }
    
    err = nvs_set_blob(handle, key, value, len);
    if (err != ESP_OK){
        ESP_LOGE(TAG, "set %s blob failed:%s",key, esp_err_to_name(err));
    }

    err |= nvs_commit(handle);
    nvs_close(handle);

    return err;
}

int GetNvsDevType(void *value, const int len)
{
    return GetNvsData(DEV_TYPE_KEY, value, len);
}

int SetNvsDevType(const void *value, const int len)
{
    return SetNvsData(DEV_TYPE_KEY, value, len);
}

int GetNvsProductID(void *value, const int len)
{
    return GetNvsData(PRODUCT_ID_KEY, value, len);
}

int SetNvsProductID(const void *value, const int len)
{
    return SetNvsData(PRODUCT_ID_KEY, value, len);
}

int GetNvsDevID(void *value, const int len)
{
    return GetNvsData(DEV_ID_KEY, value, len);
}

int SetNvsDevID(const void *value, const int len)
{
    return SetNvsData(DEV_ID_KEY, value, len);
}

int GetNvsAirConGateInfo(void *value, const int len)
{
    return GetNvsData(AIR_CON_GATE_INFO_KEY, value, len);
}

int SetNvsAirConGateInfo(const void *value, const int len)
{
    return SetNvsData(AIR_CON_GATE_INFO_KEY, value, len);
}

int GetNvsMeterInfo(void *value, const int len)
{
    return GetNvsData(METER_INFO_KEY, value, len);
}

int SetNvsMeterInfo(const void *value, const int len)
{
    return SetNvsData(METER_INFO_KEY, value, len);
}